package com.oa.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.oa.bean.work.Admin;
import com.oa.service.AdminService;
import com.oa.util.CreateAccount;
import com.oa.util.Encoding;
import com.oa.util.ResultData;
import com.oa.util.WriteResult;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 超级管理员登录
 *
 */
public class AdminAction extends ActionSupport {

	private static final long serialVersionUID = 8875649511087486804L;

	// 管理员功能service
	private AdminService adminService;

	private List<Admin> rows;

	private Admin admin;

	private int adminId;

	public int getAdminId() {
		return adminId;
	}

	public void setAdminId(int adminId) {
		this.adminId = adminId;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public List<Admin> getRows() {
		return rows;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
	public AdminAction() {
		
	}

	public String login() {
		return "login";
	}

	public String loadPage() {
		return "load";
	}

	// 增加管理员
	@SuppressWarnings("static-access")
	public String adminSave() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		admin.setAtype(2);
		admin.setAccount(CreateAccount.account("s"));
		adminService.save(admin);
		Encoding.setEncoding(request, response);
		WriteResult.write(response, new ResultData().getResultData(null, "success", "添加管理员成功！"));
		return "admin_save";
	}

	// 删除管理员
	@SuppressWarnings("static-access")
	public String adminDelete() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		adminService.delete(admin);
		Encoding.setEncoding(request, response);
		WriteResult.write(response, new ResultData().getResultData(null, "success", "删除管理员成功！"));
		return "admin_delete";
	}

	// 修改管理员
	@SuppressWarnings("static-access")
	public String adminUpdate() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		admin.setAtype(2);
		admin = adminService.update(admin);
		Encoding.setEncoding(request, response);
		WriteResult.write(response, new ResultData().getResultData(null, "success", "修改管理员信息成功！"));
		return "admin_update";
	}
	
	// 查询所有管理员
	public String adminQueryAll() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		rows = adminService.queryAll();
		Encoding.setEncoding(request, response);
		PrintWriter writer = response.getWriter();
		writer.write(JSON.toJSONString(rows));
		writer.close();
		return "admin_queryAll";
	}

	// 查询所有管理员
	public String adminQueryById() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		admin = adminService.queryById(adminId);
		Encoding.setEncoding(request, response);
		PrintWriter writer = response.getWriter();
		writer.write(JSON.toJSONString(admin));
		writer.close();
		return "admin_queryById";
		
	}

	// 分页查询管理员
	public String adminQueryPager() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String pageStr = request.getParameter("page");
		String pageSizeStr = request.getParameter("rows");
		if (pageStr != null && !"".equals(pageStr) && pageSizeStr != null && !"".equals(pageSizeStr)) {
			int page = Integer.valueOf(pageStr);
			int pageSize = Integer.valueOf(pageSizeStr);
			rows = adminService.queryByPager(page, pageSize);
		}
		// 设置编码
		Encoding.setEncoding(request, response);
		PrintWriter writer = response.getWriter();
		writer.write(JSON.toJSONString(rows));
		writer.close();
		return "admin_pager";
	}

	public String adminLogin() throws Exception {
		admin = adminService.login(admin);
		if (admin != null) {
			return "admin_login_success";
		}
		return "admin_login_fail";
	}
}
